package com.guida.controller;

import com.guida.domain.Record;
import com.guida.domain.User;
import com.guida.entity.PageResult;
import com.guida.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/record")
public class RecordController {

    @Autowired
    private RecordService recordService;

    /**
     * 查询借阅记录
     * @param record：查询条件
     * @param pageNum:当前页码
     * @param pageSize：每页显示数量
     * @return
     */
    @RequestMapping("/searchRecords")
    public ModelAndView searchRecord(Record record, Integer pageNum, Integer pageSize, HttpServletRequest request){
        //设置默认页码
        if (pageNum == null){
            pageNum = 1;
        }
        //设置默认每页显示数量
        if (pageSize == null){
            pageSize = 10;
        }
        User user = (User) request.getSession().getAttribute("USER_SESSION");
        PageResult pageResult =  recordService.searchRecords(record,user,pageNum,pageSize);
        ModelAndView mav = new ModelAndView();
        mav.setViewName("record");
        //将查询结果的数据存放到对象mav中
        mav.addObject("pageResult",pageResult);
        //将查询的参数返回到页面，用于回显
        mav.addObject("search",record);
        //将当前页码返回到页面,用于分页插件的显示
        mav.addObject("pageNum",pageNum);
        //将当前查询的控制器类返回到页面,页码变化时，继续像该路径发送请求;
        mav.addObject("gourl",request.getRequestURI());
        return mav;

    }
}
